﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Security;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Login : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
	//string encryptedstring = Convert.ToString(HttpContext.Current.Request.QueryString["estring"]);
        //string originalstring = Decrypt(encryptedstring);
        //string originalstring = "pname=anshuman&pwd=anshuman123";
        //char[] delimiterChars = { '=', '&'};
        //string[] words = originalstring.Split(delimiterChars);
        //string PName = words[1];
        //string Pwd = words[3];

        string PName = Convert.ToString(HttpContext.Current.Request.QueryString["pname"]);
        string ePwd = Convert.ToString(HttpContext.Current.Request.QueryString["pwd"]);
        string Pwd = KeyDec(ePwd, 307, 25807);

        /*int p = 131;
        int q = 197;
        int n = 0;
        n = (p * q);
        int n1 = (p - 1) * (q - 1);
        int e = 83;
        int d = 307;*/

        SqlConnection conn = null;
        SqlDataReader rdr = null;

        try
        {
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn2"].ToString());
                //Server=localhost;DataBase=PatientRecord;Integrated Security=SSPI");
            conn.Open();

            SqlCommand cmd = new SqlCommand("logincheck", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PName", PName));
            cmd.Parameters.Add(new SqlParameter("@Pwd", Pwd));


            rdr = cmd.ExecuteReader();
            if (rdr.Read())
            {
                
                    Response.Write(rdr.GetString(0));
                
            }
            else
            {
                Response.Write("No such Patient exists");
            }

        }

        finally 
        {
            if (conn != null)
            {
                conn.Close();
            }
            if (rdr != null)
            {
                rdr.Close();
            }
       
        }

        
    }

    static string KeyDec(string CipherText, int d, int n)
    {
        string DecryptedString = "";
        int ProcessedLength = 0;
        int i, X;
        long Y;

        while (ProcessedLength < CipherText.Length)
        {
            string Number = CipherText.Substring(ProcessedLength, 8);
            int lNumber = Convert.ToInt32(Number);
            Y = Crypt(lNumber, d, n);

            DecryptedString += (char)Y;
            ProcessedLength += 8;
        }
        return DecryptedString;
    }


    static long isPrime(long n)
    {
        long b = 1;
        for (long i = 2; i < n; i++)
            if (n % i == 0) { b = 0; break; }
        return b;
    }
    static long Power(long x, long y)
    {
        long ans = 1;
        if (x == 1) return x;
        for (long i = 1; i <= y; i++)
            ans *= x;
        return (ans);
    }
    static string ToBin(int x)
    {
        char[] ans = new char[20];
        int i = 0;
        while (x >= 1)
        {
            if (x % 2 == 1) ans[i] = '1'; else ans[i] = '0';
            x /= 2; i++;
        }
        if (x == 1)
            ans[i] = '1';
        i++;
        //ans[i]='\0';

        Array.Reverse(ans);
        string ans1 = new string(ans);
        return ans1;
    }
    static long Crypt(int x, int key, int n) // (x^y)%z //Method 2
    {
        string B;
        B = ToBin(key);

        long ans = 1; int c = 1;
        for (int i = 0; i < B.Length; i++)
        {
            c = 2 * c;
            ans = (ans * ans) % n;
            if (B[i] == '1')
            {
                c = c + 1;
                ans = (ans * x) % n;
            }
        }
        return (ans);
    }

    static long Bin2Num(string s)
    {
        long ans = 0; int i, j;
        for (j = 0, i = 7; i >= 0; j++, i--)
        {
            if (s[i] == '1')
                ans += Power((long)2, (long)j);
        }
        return ans;
    }
    //public static string Decrypt(string cryptedString)
    //{
    //    if (String.IsNullOrEmpty(cryptedString))
    //    {
    //        throw new ArgumentNullException
    //           ("The string which needs to be decrypted can not be null.");
    //    }
    //    byte[] bytes = ASCIIEncoding.ASCII.GetBytes("acns123");
    //    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    //    MemoryStream memoryStream = new MemoryStream
    //            (Convert.FromBase64String(cryptedString));
    //    CryptoStream cryptoStream = new CryptoStream(memoryStream,
    //        cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
    //    StreamReader reader = new StreamReader(cryptoStream);
    //    return reader.ReadToEnd();
    //}
}
